package com.example.neusoft.cook.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.neusoft.cook.R;
import com.example.neusoft.cook.ViewHolder.ShoopFavorListViewHolder;
import com.example.neusoft.cook.activity.FoodListActivity;
import com.example.neusoft.cook.base.BaseAcyivity;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.base.BaseListAdapter;
import com.example.neusoft.cook.bean.FavorShop;
import com.example.neusoft.cook.bean.SampleFavor;
import com.example.neusoft.cook.bean.SampleShop;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by neusoft on 2017/5/24.
 */

public class ShopFavorListAdapter extends BaseListAdapter {
    public ShopFavorListAdapter(Context context, List data) {
        super(context, data);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateVH(ViewGroup parent, LayoutInflater layoutInflater, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_shop_favorite,parent,false);
        ShoopFavorListViewHolder viewHolder = new ShoopFavorListViewHolder(itemView);
        return viewHolder;
    }

    @Override
    protected void onBindVH(RecyclerView.ViewHolder holder, List data, final int position) {
        ShoopFavorListViewHolder viewHolder = (ShoopFavorListViewHolder) holder;
        final SampleFavor sampleFavor = (SampleFavor) data.get(position);
        final int shopId = sampleFavor.getShop_id();

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动新的Activity--店铺详情（菜品列表）
                Toast.makeText(context,"店铺详情"+position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, FoodListActivity.class);
                intent.putExtra("shop_id",shopId);
                intent.putExtra("shop_name",sampleFavor.getShopname());
                context.startActivity(intent);

            }
        });
        viewHolder.shopNameView.setText(sampleFavor.getShopname());
        viewHolder.shopAddressView.setText(sampleFavor.getAddress());
        Picasso.with(context)
                .load(sampleFavor.getPic())
                .into(viewHolder.imageView);
        viewHolder.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFavor(String.valueOf(sampleFavor.getUser_id()),String.valueOf(sampleFavor.getShop_id()));
            }

            private void doFavor(String userId, String shopId) {
                //定义Retrofit对象
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Config.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                //获取Retrofit接口
                RetrofitService retrofitService = retrofit.create(RetrofitService.class);
                Call<FavorShop> call =retrofitService.userCollectShop(userId,shopId);
                call.enqueue(new Callback<FavorShop>() {
                    @Override
                    public void onResponse(Call<FavorShop> call, Response<FavorShop> response) {
                        if (response.body().getSuccess().equals("1")){
                            //操作成功
                            Toast.makeText(context,"取消收藏成功",Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFailure(Call<FavorShop> call, Throwable t) {

                    }
                });
            }
        });

    }
}
